/*
 * GUI.cpp
 *
 * Author: Samuel Queiroz
 */

#include "GUI.h"
#include <cstdlib>
#include <sys/time.h>
#include <stdio.h>

void GUI::run()
{
	double t_now;
	double t_print = 0;
	struct timeval  tv;

	while (! stop_)
	{
		gettimeofday(&tv, NULL);
		t_now = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;

		if ((t_now - t_print) >= 100)
		{
			t_print = t_now;

			system("clear");

			printf("┌────────────────────────────────────────────┐\n");
			printf("│░░░░░░▒▒▒▒▒▒░░░▒▒▒▒▒▒░░░▒▒▒▒▒▒░░░▒▒▒▒▒░░░░░░│\n");
			printf("│░░░░░░▒▒░░░░░░░▒▒░░░░░░░▒▒░░▒▒░░░▒▒░░▒▒░░░░░│\n");
			printf("│░░░░░░▒▒▒▒▒▒░░░▒▒░░░░░░░▒▒░░▒▒░░░▒▒▒▒▒░░░░░░│\n");
			printf("│░░░░░░░░░░▒▒░░░▒▒░░░░░░░▒▒░░▒▒░░░▒▒░▒▒░░░░░░│\n");
			printf("│░░░░░░▒▒▒▒▒▒░░░▒▒▒▒▒▒░░░▒▒▒▒▒▒░░░▒▒░░▒▒░░░░░│\n");
			printf("│                                            │\n");
			printf("│        S P O R T  C O L L E C T I F        │\n");
			printf("│            P O U R  R O B O T S            │\n");
			printf("│--------------------------------------------│\n");
			printf("│                                            │\n");
			printf("│           ╔══════════╗                ╔═╗  │\n");
			printf("│     LEFT  ║%s║ RIGHT       S  ║%s║  │\n", robotSwitcher->isRight() ? "     ▒▒▒▒▒" : "▒▒▒▒▒     ", getPosition(speedSlider, 5) == 5 ? "*" : "·");
			printf("│           ╚══════════╝             P  ║%s║  │\n", getPosition(speedSlider, 5) == 4 ? "*" : "·");
			printf("│           ╔══════════╗             E  ║%s║  │\n", getPosition(speedSlider, 5) == 3 ? "*" : "·");
			printf("│  KEYBOARD ║%s║ JOYSTICK    E  ║%s║  │\n", controlSwitcher->isRight() ? "     ▒▒▒▒▒" : "▒▒▒▒▒     ", getPosition(speedSlider, 5) == 2 ? "*" : "·");
			printf("│           ╚══════════╝             D  ║%s║  │\n", getPosition(speedSlider, 5) == 1 ? "*" : "·");
			printf("│                                       ╚═╝  │\n");
			printf("│                                      %2d/%2d │\n", speedSlider->getValue(), speedSlider->getUpperBound());
			printf("│                                            │\n");
			printf("│                                            │\n");
			printf("│                                    R  ╔═╗  │\n");
			printf("│                                    O  ║%s║  │\n", getPosition(rotationSlider, 5) == 5 ? "*" : "·");
			printf("│               ╔════╗               T  ║%s║  │\n", getPosition(rotationSlider, 5) == 4 ? "*" : "·");
			printf("│               ║%s║               A  ║%s║  │\n", upButton->isPressed() ? " ↑  " : "    ", getPosition(rotationSlider, 5) == 3 ? "*" : "·");
			printf("│               ╚════╝               T  ║%s║  │\n", getPosition(rotationSlider, 5) == 2 ? "*" : "·");
			printf("│        ╔════╗ ╔════╗ ╔════╗        I  ║%s║  │\n", getPosition(rotationSlider, 5) == 1 ? "*" : "·");
			printf("│        ║%s║ ║%s║ ║%s║        O  ╚═╝  │\n", leftButton->isPressed() ? " ←  " : "    ", downButton->isPressed() ? " ↓  " : "    ", rightButton->isPressed() ? " →  " : "    ");
			printf("│        ╚════╝ ╚════╝ ╚════╝        N %2d/%2d │\n", rotationSlider->getValue(), rotationSlider->getUpperBound());
			printf("│                                            │\n");
			printf("│--------------------------------------------│\n");
			printf("│                                            │\n");
			printf("└────────────────────────────────────────────┘\n");
		}
	}
}

int GUI::getPosition(Slider *slider, int nUnits)
{
	return ((slider->getValue() -1) / (slider->getUpperBound() / nUnits)) + 1;
}
